#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv\cv.h>
#include <opencv2\opencv.hpp>
#include <opencv2/video/background_segm.hpp>

using namespace cv;
using namespace std;

//int main(int argc, char *argv[])
//{
//    cv::Mat frame;
//    cv::Mat back;
//    cv::Mat fore;
//    cv::VideoCapture capture("cameraGiaoThong3FPS18.avi");//mv2_001.avi
//    cv::BackgroundSubtractorMOG2 bg = BackgroundSubtractorMOG2();
////    bg.nmixtures = 3;
////   bg.bShadowDetection = false;
// 
//    std::vector<std::vector<cv::Point> > contours;
//    cv::namedWindow("fore"); 
//    cv::namedWindow("Frame");
//    cv::namedWindow("Background");
//	int i = 0;
//    for(;;)
//    {
//		i++;
//		cout<< i<<endl;
//        capture >> frame;
//		bg.operator ()(frame,fore);
//        bg.getBackgroundImage(back);
//        cv::erode(fore,fore,cv::Mat());
//        cv::dilate(fore,fore,cv::Mat());
//        //cv::findContours(fore,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
////        cv::drawContours(frame,contours,-1,cv::Scalar(0,0,255),2);
//		//cv::threshold(fore,back, 128,255,cv::THRESH_BINARY_INV);
//        //cv::erode(fore,fore,20);
//        //cv::dilate(fore,fore,10);
//		cv::imshow("Frame",frame);
//        cv::imshow("fore",fore);
//        cv::imshow("Background",back);
//        if(cv::waitKey(1000/18) >= 0) break;
//    }
//    return 0;
//}

int main()
{
	int vehicle = 0;
	int road = 0;
   // Open the video file
    cv::VideoCapture capture("camera.mp4");
   // check if video successfully opened
   if (!capture.isOpened())
      return 0;
   cv::Mat frame; 
   cv::Mat background;
   cv::Mat foreground;

   cv::Mat gray_road;
   cv::Mat gray_background;
   cv::Mat gray_foreground;

   cv::namedWindow("Background");
   cv::namedWindow("Extracted Foreground");
   // The Mixture of Gaussian object
   // used with all default parameters
   cv::BackgroundSubtractorMOG2 mog(10, 9,false);
   bool stop(false);
   // for all frames in video
   while (!stop) {
      // read next frame if any
      if (!capture.read(frame))
         break;
	  // update the background and return the foreground
	  mog(frame,foreground,0.1);
	  //Get the background
	  mog.getBackgroundImage(background);

      // Complement the image  
	  cv::threshold(foreground,foreground, 20,255,cv::THRESH_BINARY);
	  
	  //medianBlur(foreground, foreground, 3);
	  //cv::erode(foreground,foreground,36);
	  //cv::dilate(foreground,foreground,20);
      
	  cv::imshow("Background",background);
	  cv::imshow("Extracted Foreground",foreground);
	  //cv::cvtColor(background, gray_background, CV_BGR2GRAY);
	  //cv::cvtColor(background, gray_road, CV_BGR2GRAY);
	  //cv::cvtColor(background, gray_foreground, CV_BGR2GRAY);


	  //cv::accumulateWeighted(gray_foreground, gray_road , 1, gray_background);
	  // introduce a delay
      // or press key to stop
      if (cv::waitKey(20)>=0)
            stop= true;
   }
}